extends BoarStatusBase


var period_time:float = 0
var loss_turn_time:float = 0
var loss_num:int = 0
var hurt_self_speed:float = 0
func start():
	period_time = 0
	loss_turn_time = 0
	loss_num = 0
	
	actor.play_animation("Run")
	if hurt_self_speed == 0:
		hurt_self_speed = blackboard.max_speed * 1.5 - 1
	
func _period_time(delta:float) -> float:
	if period_time >= 0.1:
		period_time = 0
	else:
		period_time += delta
	return period_time
		
func run_physics_process(delta)->void:
	var period_time:float = _period_time(delta)
	if period_time == 0.0:
		# 避免频繁撞击
		if not find_floor():
			turn_direction()
			return
		elif find_wall():
			turn_direction()
			return
			
	# 撞墙墙受伤
	var pre_volocity = actor.velocity
	actor.move_and_slide()
	var volocity = actor.velocity
	if actor.is_on_wall():
		var speed = abs(volocity.x - pre_volocity.x)
		if speed > hurt_self_speed:
			actor.hurt()
			return
			
	# 发现玩家继续跑
	if find_player():
		loss_turn_time = 0
		loss_num = 0
		return 

	# 丢失玩家三次变成 walk 状态
	if loss_num > 3:
		return change_status("Walk")
	
	# 转向逻辑 丢失玩家累计1.2秒后转向
	loss_turn_time += delta
	if loss_turn_time > 1.2:
		loss_turn_time = 0
		loss_num += 1
		turn_direction()
		return
		
func run_process(delta)->void:
	common_velocity(delta,1.5)
	#print(actor.velocity.x)
	
 
